/**
 * @Description: UserController
 * @Author: Mr.Niu
 * @Date: 2022/9/20 17:18
 */
package com.zhiyou.crud.controller;
import com.zhiyou.crud.bean.User;
import com.zhiyou.crud.service.NewsService;
import com.zhiyou.crud.service.UserService;
import com.zhiyou.util.DBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;


@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    NewsService newsService;
    @Autowired
    DBUtil dbUtil;
    @GetMapping("/login")
    public String getUser(User user, HttpSession session , Model model){
        Integer[] getallnews=newsService.getddj();
        session.setAttribute("ddj", getallnews);
        //用户 登录
        if (!user.getUphone().equals("")&!user.getUpassword().equals("")) {
            dbUtil.judge(user);
            User getUser = userService.getSelectUser(user);
            System.out.println("getUser = " + getUser);
            if (getUser != null) {
                //登录成功页面
                session.setAttribute("user",getUser);
                return "redirect:/index.jsp";
            }
        }
        //登录失败提示并返回页面
        model.addAttribute("msg","登陆失败");
        return "redirect:/login.jsp";
    }
// 获取验证码
    @RequestMapping("/sms")
//    @ResponseBody
    public void SMS(User user, String pwd, HttpSession session, HttpServletResponse response) throws Exception {
        response.setContentType("text/json:charset=utf-8");
        PrintWriter out=response.getWriter();
        if (user.getUphone()!=null&!user.getUname().equals("")&!user.getUpassword().equals("")&!pwd.equals("")&user.getUpassword().equals(pwd)){
            User getUser = userService.getSelectUser(user);
            if (getUser==null){
            dbUtil.judge(user);
            if (user.getUphone()!=null){
                session.setAttribute("suser",user);
                //获取的验证码
                String sms = dbUtil.SMS(user)+"";
                System.out.println("sms = " + sms);
                session.setAttribute("code",sms);
                //二次密码
                session.setAttribute("pwd",pwd);
                String resStr="{\"response\":\"success\"}";
                out.println(resStr);
            }else {
                session.setAttribute("suser",user);
                //获取的验证码
                String sms = dbUtil.SPI(user)+"";
                System.out.println("sms = " + sms);
                session.setAttribute("code",sms);
                //二次密码
                session.setAttribute("pwd",pwd);
                String resStr="{\"response\":\"success\"}";
                out.println(resStr);

            }

            }else {
                String resStr="{\"response\":\"error\"}";
                out.println(resStr);
            }
        }else {
            String resStr="{\"response\":\"sb\"}";
            out.println(resStr);
        }
    }
    //    用户注册
        @RequestMapping("/enroll")
        public String addUser(User user,String pwd,HttpSession session ,String code) {
            System.out.println("ssssssssssssssssssssssssss");
            dbUtil.judge(user);
            User suser = (User) session.getAttribute("suser");
            String scode = (String) session.getAttribute("code");
            String spwd = (String) session.getAttribute("pwd");
            if (
                    user.equals(suser)
                    &pwd.equals(spwd)
                    &code.equals(scode))
            {
                System.out.println(" =====================");
                int i = userService.addUser(user);
                System.out.println("i = " + i);
                return "/login";
            }
            return "/login";
        }
        //退出登录
        //用于退出页面
        @RequestMapping("tologout")
        public String tologout(HttpSession session){
        session.invalidate();
            return "/login";
        }
}
